package com.yanxu;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/6/8-2025
 * @Description: 这里用一句话描述这个类的作用
 */
public class Test1 {
    public static void main(String[] args) throws IOException {
        // 使用 put 方法写入数据
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(1024);
        byteBuffer1.put("yanxu".getBytes());
        byteBuffer1.putChar('a');
        byteBuffer1.putInt(10);
        byteBuffer1.putLong(123456789111l);

        byteBuffer1.flip();
        byte[] bytes = new byte[128];
        //byteBuffer1.get();  // 读取一个字节
        //byteBuffer1.get(10)
        byteBuffer1.get(bytes, 0, 5);
        System.out.println(new String(bytes,0,5));
        System.out.println(byteBuffer1.getChar());
        System.out.println(byteBuffer1.getInt());
        System.out.println(byteBuffer1.getLong());

        // 通过channel 写入数据
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);
        FileChannel fin = new FileInputStream(new File("")).getChannel();
        while (fin.read(byteBuffer2) > 0) ;
    }
}